package GraphTheory;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Queue;
import java.util.TreeMap;

class Query
{	public int m1,m2,m3;
	public Query(int i1,int i2,int i3)
	{	m1=i1;m2=i2;m3=i3;
	}
}

class Solver1837
{	int n,count;
	int[][] Team;
	boolean hasRoot=false;
	boolean[] Visited;
	Query[] Qry;
	Map<String,Integer> IndexMap;
	Map<Integer,String> NameMap;
	Map<String,Integer> NumMap;
	
	private int AddIndex(StreamTokenizer in) throws IOException
	{	in.nextToken();
		String name=in.sval;
		if (name.equals("Isenbaev"))
			hasRoot=true;
		if (!IndexMap.containsKey(name))
		{	count++;
			IndexMap.put(name,count);
			NameMap.put(count,name);
			return count;
		}
		else
			return IndexMap.get(name);
	}
	public void Input() throws IOException
	{	StreamTokenizer in=new StreamTokenizer(new InputStreamReader(System.in));
		in.nextToken();n=(int)in.nval;
		Comparator<String> StrCMP=new Comparator<String>()
		{	public int compare(String s1,String s2)
			{	return s1.compareTo(s2);
			}
		};
		
		count=1;
		Qry=new Query[n+1];
		IndexMap=new HashMap<String,Integer>();
		NumMap=new TreeMap<String,Integer>(StrCMP);
		NameMap=new HashMap<Integer,String>();
		IndexMap.put("Isenbaev",count);
		NameMap.put(count,"Isenbaev");
		for (int i=1;i<=n;i++)
		{	int i1,i2,i3;
			i1=AddIndex(in);i2=AddIndex(in);i3=AddIndex(in);
			Qry[i]=new Query(i1,i2,i3);
		}
	}
	private void Prepare()
	{	int sz=IndexMap.size();
		Team=new int[sz+1][sz+1];
		Visited=new boolean[sz+1];
		
		for (int i=1;i<=sz;i++)
		{	Visited[i]=false;
			for (int j=1;j<=sz;j++)
				Team[i][j]=0;
		}
		for (int i=1;i<=n;i++)
		{	Team[Qry[i].m1][Qry[i].m2]=1;Team[Qry[i].m2][Qry[i].m1]=1;
			Team[Qry[i].m1][Qry[i].m3]=1;Team[Qry[i].m3][Qry[i].m1]=1;
			Team[Qry[i].m2][Qry[i].m3]=1;Team[Qry[i].m3][Qry[i].m2]=1;
		}
	}
	public void Solve()
	{	Queue<Integer> queue=new LinkedList<Integer>();
		
		Prepare();
		queue.add(1);Visited[1]=true;NumMap.put(NameMap.get(1),0);
		while (!queue.isEmpty())
		{	int focus=queue.remove();
			for (int i=1;i<=Visited.length-1;i++)
				if (!Visited[i])
				{	if (Team[focus][i]==1)
					{	queue.add(i);
						NumMap.put(NameMap.get(i),NumMap.get(NameMap.get(focus))+1);
						Visited[i]=true;
					}
				}
		}
		for (int i=1;i<=Visited.length-1;i++)
			if (!Visited[i])
				NumMap.put(NameMap.get(i),-1);
	}
	public void Output()
	{	String udf="undefined";
		for (Entry<String,Integer> e:NumMap.entrySet())
		{	if (e.getValue()==0 && !hasRoot) continue;
			System.out.print(e.getKey()+" ");
			if (e.getValue()!=-1)
				System.out.println(e.getValue());
			else
				System.out.println(udf);
		}
	}
	public void main() throws IOException
	{	Input();
		Solve();
		Output();
	}
}
public class P1837
{	public static void main(String[] args) throws IOException
	{	Solver1837 solver=new Solver1837();
		solver.main();
	}
}
